package jh.com.jhappbf.contract;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.model.PriceModel;
import jh.com.jhappbf.utils.ExpressionUtils;
import jh.com.jhappbf.utils.JsonHelper;

public class PriceUpdate extends AbActivity {
    private TextView mTxtId;
    private TextView mTxtReceiver;
    private TextView mTxtCargoname;
    private EditText mTxtPrice;
    private AbTitleBar mTitleBar;
    private Context mAppContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.price_set_activity);
        mTitleBar = getTitleBar();
        mTitleBar.setTitleText("单价设置");
        mTitleBar.setLogo(R.drawable.button_selector_back);
        mTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mTitleBar.setLogoLine(R.mipmap.line);
        mAppContext = getApplicationContext();
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        mTxtId = findViewById(R.id.price_id_txt);
        mTxtReceiver = findViewById(R.id.price_receiver_txt);
        mTxtCargoname = findViewById(R.id.price_carogname_txt);
        mTxtPrice = findViewById(R.id.price_price_edit);
        View rightView = View.inflate(this,R.layout.title_right_ok_btn_view, null);
        mTitleBar.addRightView(rightView);
        Button mBtnOk = rightView.findViewById(R.id.title_right_ok_btn);
        mBtnOk.setOnClickListener(v -> {
            String price = mTxtPrice.getText().toString();
            String id = mTxtId.getText().toString();
            if (!ExpressionUtils.isNumber(price)) {
                Toast.makeText(PriceUpdate.this, "输入的单价不合法!", Toast.LENGTH_SHORT).show();
            } else {
                operatorServer(id, price);
            }
        });
        setDefaultValue();
    }

    /**
     * 添加默认值
     */
    private void setDefaultValue() {
        Intent i = getIntent();
        PriceModel model = i.getParcelableExtra("data");
        if (model != null) {
            mTxtId.setText(model.getId());
            mTxtReceiver.setText(model.getReceiver());
            mTxtCargoname.setText(model.getCargoname());
            mTxtPrice.setText(model.getPrice());
        }
    }


    /**
     * 第一次加载
     */
    private void operatorServer(final String id, final String price) {
        AbLoadDialogFragment mDialogFragment = AbDialogUtil.showLoadDialog(PriceUpdate.this,
                R.mipmap.ic_load, "操作中,请稍等...");
        mDialogFragment.setCancelable(false);
        mDialogFragment.setAbDialogOnLoadListener(() -> setPrice(id, price));
    }

    /**
     * 设置单价
     */
    private void setPrice(String id, String price) {
        String methodName = "updatePriceOnly";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("id");
        param.add("price");
        value.add(id);
        value.add(price);

        Listener<String> listener = response -> {
            AbDialogUtil.removeDialog(PriceUpdate.this);
            List<JSONObject> newListData = JsonHelper.getJsonObjects(response);
            if (newListData != null && newListData.size() > 0) {
                JSONObject obj = newListData.get(0);
                String value1 = obj.optString("result");
                String memo = obj.optString("memo");
                if ("true".equals(value1)) {
                    PriceUpdate.this.setResult(RESULT_OK);
                    PriceUpdate.this.finish();
                } else {
                    Toast.makeText(PriceUpdate.this, memo, Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(PriceUpdate.this, "操作失败,请重试", Toast.LENGTH_SHORT).show();
            }
        };

        ErrorListener errorListener = error -> {
            AbDialogUtil.removeDialog(PriceUpdate.this);
            Toast.makeText(PriceUpdate.this, "操作失败,请重试", Toast.LENGTH_SHORT).show();
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(mAppContext).addRequest(webService);
    }
}
